var page = 1;
var limit = 5;
var totalCount = 0;
var totalPage = 0;
$(function(){
	if(user == null || user == ""){
		$("#loginOrRegist").show();
	}else {
		$("#usersName").show();
		$("#top-index-usersName").text(user.usersName);
	}
	
	//得到产品ID
	var proId = getParameter("proId");
	//查询商品滚动图片
	var data = {"proId":proId,"imgType":"2","page":page,"limit":limit};
	findImage(data,'pro-gallerys');
	//查询商品详情图片
	var data = {"proId":proId,"imgType":"3","page":"1","limit":"20"};
	findImage(data,'kindPicture-10086431508342');
	
	//查询商品信息
	var url = "../product/findById.action";
	var data = {"proId":proId};
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			$("#proName").val(msg.proName);
			$("#pro-price-hide").val(msg.proPrice);
			$("#proColor").val(msg.proColor);
			$("#proCapacity").val(msg.proCapacity);
			$("#pro-name").html(msg.proName);
			$("#pro-price").append(msg.proPrice);
			$("#product_color").html('<li class="selected" data-attrname="颜色"'
										+'><div class="sku">'
										+'<a href="javascript:;" title="'+msg.proColor+'">'
										+'<p><span>'+msg.proColor+'</span></p></a></div></li>');
			$("#product_capacity").html('<li class="selected" data-attrname="版本"'
										+'><div class="sku">'
										+'<a href="javascript:;" title="'+msg.proCapacity+'"><p>'
										+'<span>'+msg.proCapacity+'</span></p></a></div></li>');
			$("#pro-select-sku").html(msg.proColor+'/'+msg.proCapacity+'/官方标配');
		}
	});
	//查询商品评论
	showComment();
})
//查询商品图片的方法
function findImage(data,str){
	var url = "../productImage/findByProIdAllImage.action";
	$.ajax({
		url:url,
		data:data,
		async:false,
		success:function(msg){
			$("#"+str).html("");
			var list = msg.data;
			if(str == "pro-gallerys"){
				totalCount = msg.count;
				for(var i=0;i<list.length;i++){
					if(i == 0){
						$("#"+str).append('<li class="current"><a href="javascript:;" onclick="gdImage(this);"><img src="../'+list[i].imgPath+'"'
								+'alt="'+list[i].proName+'"></a></li>');
					}else {
						$("#"+str).append('<li class=""><a href="javascript:;" onclick="gdImage(this);"><img src="../'+list[i].imgPath+'"'
								+'alt="'+list[i].proName+'"></a></li>');
					}
					
				}
				//得到总页数
				if(totalCount % 5 == 0){
					totalPage = totalCount / 5;
				}else {
					totalPage = (totalCount / 5) + 1;
				}
				$(".page").click(function(){
					var obj = event.target;
					var action = $(obj).attr("action");
					if(action == "perv"){
						if(page > 1){
							page = page - 1;
							var data = {"proId":getParameter("proId"),"imgType":"2","page":page,"limit":limit};
							findImage(data,"pro-gallerys");
						}
					}else if(action == "next"){
						if(page < parseInt(totalPage)){
							page = page + 1;
							var data = {"proId":getParameter("proId"),"imgType":"2","page":page,"limit":limit};
							findImage(data,"pro-gallerys");
						}
					}
				});
			}else if(str == "kindPicture-10086431508342"){
				for(var i=0;i<list.length;i++){
					$("#"+str).append('<p><img src="../'+list[i].imgPath+'" alt="'+list[i].proName+'" title="undefined"></p>');
				}
			}
			
		}
	});
	
	var li = $("li.current");
	var imgPath = $($(li).find("img")).attr("src");
	var alt = $($(li).find("img")).attr("alt");
	$($("#product-img").find("img")).attr("src",imgPath);
	$($("#product-img").find("img")).attr("alt",alt);
}
function gdImage(obj){
	$($("li.current")).attr("class","");
	$($(obj).parent()).attr("class","current");
	var imgPath = $($(obj).find("img")).attr("src");
	var alt = $($(obj).find("img")).attr("alt");
	$($("#product-img").find("img")).attr("src",imgPath);
	$($("#product-img").find("img")).attr("alt",alt);
}
//商品数量
function pro_number(str){
	var num = $("#pro-quantity").val();
	if(str == "jia"){
		num = parseInt(num) + 1;
	}else if(str == "jian"){
		num = parseInt(num) - 1;
		if(num <= 0){
			num = 1;
		}
	}
	$("#pro-quantity").val(num);
}
//确认订单
function confirmOrder(){
	if(user != null && user != ""){
		var proQuantity = $("#pro-quantity").val();
		location.href = "confirmOrder.html?proId="+getParameter('proId')+"&proQuantity="+proQuantity
	}else {
		alert('请登录');
	}
}
layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
	  var laydate = layui.laydate //日期
	  ,laypage = layui.laypage //分页
	  layer = layui.layer //弹层
	  ,table = layui.table //表格
	  ,upload = layui.upload //上传
	  ,element = layui.element; //元素操作
	  
	  var $ = layui.$;
	  //登录
	  $("#top-index-loginUrl").click(function(){
		  layer.open({
			  title: ' '
			  ,type: 2
			  ,area: ['38%','74%']
			  ,content: 'login.html'
			});
	  });
	  //注册
	  $("#top-index-registUrl").click(function(){
		  layer.open({
			  title: ' '
			  ,type: 2
			  ,area: ['38%','74%']
			  ,content: 'regist.html'
			});
	  });
	  
	  $("#addCart").click(function(){
		  if(user != null && user != ""){
				var proQuantity = $("#pro-quantity").val();
				var url = "../shoppingCar/save.action";
				var data = {"proId":getParameter('proId'),"productNumber":proQuantity,"productPrice":$("#pro-price-hide").val(),"productColor":$("#proColor").val(),"productCapacity":$("#proCapacity").val()};
				$.ajax({
					url:url,
					data:data,
					success:function(msg){
						if(msg.state == "1"){
							layer.open({
								  content: '加入购物车成功!'
								  ,btn: ['再逛逛', '去结算']
								  ,yes: function(index, layero){
									  location.href = "index.html";
								  }
								  ,btn2: function(index, layero){
								    location.href = "confirmOrder.html?proId="+getParameter('proId')+"&proQuantity="+proQuantity;
								  }
								});
						}
					}
				});
			}else {
				alert('请登录');
			}
	  });
	  
	});
//查询商品评论
function showComment(){
	var proId = getParameter('proId');
	var data = {"proId":proId};
	var url = "../comment/findAll.action";
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			$("#pro-msg-list").html("");
			for(var i=0;i<msg.length;i++){
				$("#pro-msg-list").append('<li class="clearfix"><div class="product-comment-user"><p class="product-comment-user-img">'
									+'<img src="../'+msg[i].userImage+'" alt="'+msg[i].userName+'"><i'
									+' class="icon-vip-level-2"></i></p><p class="product-comment-user-name">'+msg[i].userName+'</p></div>'
									+'<div class="product-user-comment"><div class="product-user-comment-detial"><div class="h clearfix">'
									+'<div class="product-star"><span class="starRating-area"><s style="width: 100%"></s></span></div>'
									+'<div class="product-user-comment-impress"><dl></dl></div></div>'
									+'<div class="product-user-comment-word" id="10086785341226-56130791507826">'+msg[i].commentContent+'</div>'
									+'<div class="product-user-comment-time">'+msg[i].commentDate+'</div></div></div></li>');
			}
		}
	});
}